<?php
/**
 * 基础设置
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
use Model\SettingMod;
use Plug\Plug;
use Pub\cache\Cache;
use Pub\tool\Setting as SettingCache;
use Pub\wx\WxTpl;

class Setting extends Action
{
    private $Setting = null;
    public function __construct()
    {
        parent::__construct();
        $this->Setting = new SettingMod();
    }

    /**
     * 设置参数
     * @param array data 参数
     */
    public function set()
    {
        SettingCache::clear();
        $_setting = $this->Setting->findOne(array("uniacid"=>$this->_uniacid),array("id"));
        if($_setting){
            $_result = $this->Setting->update($this->_gpc["data"],array("uniacid"=>$this->_uniacid));
        }else{
            $this->_gpc["data"]["uniacid"] = $this->_uniacid;
            $_result = $this->Setting->insert($this->_gpc["data"]);
        }
        if($_result) $this->_result = array("status"=>"success");
        $this->result($this->_result);
    }


    public function easyFind()
    {
        Cache::remove(Cache::TEMP_BIND_ADMIN_UID,array("uniacid"=>$this->_uniacid));
        $_return = $this->_gpc['return'];
        $_setting = array();
        foreach($_return as $_v){
            $_setting[$_v] = SettingCache::get($_v);
        }
        $this->_result = array('status'=>'success','data'=>$_setting);
        $this->result($this->_result);
    }

    /**
     * 系统是否有酷源防封模块
     */
    public function testDdfend()
    {
        $Plug = new Plug(Plug::DEFEND);
        if($Plug->check()) $this->_result = array("status"=>"success");
        $this->result($this->_result);
    }

    /**
     * 获取绑定管理员二维码
     */
    public function getBindAdminQrcode()
    {
        session_start();
        $_sign = sha1(uniqid(TIMESTAMP));
        Cache::set(Cache::BIND_ADMIN_SIGN,$_sign,array("uniacid"=>$this->_uniacid));
        $_url = murl('entry//scan',array('m'=>'kuyuan_red','type'=>'bind-admin','sign'=>$_sign,'uniacid'=>$this->_uniacid),true,true);
        $_qrcode = $this->getQrcode("bind_admin_qrcode","qrcode",$_url,true);
        $this->_result = array("status"=>"success","data"=>$_qrcode.'?time='.TIMESTAMP);
        unset($_qrcode);
        $this->result($this->_result);
    }

    /**
     * 获取临时系统管理员信息
     */
    public function getTempAdmin()
    {
        $_cache_uid = Cache::get(Cache::TEMP_BIND_ADMIN_UID,array("uniacid"=>$this->_uniacid));
        if($_cache_uid){
            $_member = $this->uidToInfo($_cache_uid);
            $_data = array("uid"=>$_cache_uid,"header"=>$_member["avatar"]);
            $this->_result = array("status"=>"success","data"=>$_data);
        }
        $this->result($this->_result);
    }

    /**
     * 获取公众号模板消息行业信息
     */
    public function getTplMessageIndustry()
    {
        $Wx = new WxTpl();
        $_industry = $Wx->getIndustry();
        $_industry = json_decode($_industry,true);
        if(isset($_industry["primary_industry"])){
            $this->_result = array("status"=>"success","data"=>$_industry);
        }
        $this->result($this->_result);
    }

    /**
     * 自动获取模板消息
     * @param set_industry 是否设置行业
     */
    public function autoSetTpl()
    {
        $Wx = new WxTpl();
        //先删除现有的模板消息ID
        $_tpls = $this->Setting->findOne(array("uniacid"=>$this->_uniacid),array(
            "message_media_check",
            "message_media_pass"
        ));
        if($_tpls){
            $Wx->remove($_tpls);
        }
        $_data = $Wx->get();
        if($_data["status"] == "success"){
            $this->_result = array("status"=>"success","data"=>$_data["data"]);
        }else{
            $this->_result = array("status"=>"error","err_msg"=>$_data["data"]["errmsg"]);
        }
        $this->result($this->_result);
    }
}